package com.sqllog.admin.auth.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sqllog.admin.auth.domain.User;
import com.sqllog.admin.auth.mapper.UserMapper;
import com.sqllog.admin.framework.domain.request.PageDomain;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户服务实现类
 *
 * @author chennengcheng
 * @date 2022-01-14 14:35
 * @remark
 */

@Service
public class UserService {

    /**
     * 注入用户服务
     */
    @Resource
    private UserMapper userMapper;


    /**
     * Describe: 根据条件查询用户列表数据
     * Param: username
     * Return: 返回用户列表数据
     */
    public List<User> list(User param) {
        return userMapper.selectList(param);
    }

    /**
     * Describe: 根据条件查询用户列表数据  分页
     * Param: username
     * Return: 返回分页用户列表数据
     */
    public PageInfo<User> page(User param, PageDomain pageDomain) {
        PageHelper.startPage(pageDomain.getPage(), pageDomain.getLimit());
        List<User> sysUsers = userMapper.selectList(param);
        return new PageInfo<>(sysUsers);
    }

    /**
     * Describe: 根据 ID 查询用户
     * Param: id
     * Return: 返回用户信息
     */
    public User getById(String id) {
        return userMapper.selectById(id);
    }

    /**
     * Describe: 根据 id 删除用户数据
     * Param: id
     * Return: Boolean
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean remove(String id) {
        userMapper.deleteById(id);
        return true;
    }

    /**
     * Describe: 根据 id 批量删除用户数据
     * Param: ids
     * Return: Boolean
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean batchRemove(String[] ids) {
        userMapper.deleteByIds(ids);
        return true;
    }

    /**
     * Describe: 保存用户数据
     * Param: User
     * Return: 操作结果
     */
    public boolean save(User sysUser) {
        User user = new User();
        user.setUsername(sysUser.getUsername());
        if (userMapper.count(user) > 0) {
            return false;
        }
        int result = userMapper.insert(sysUser);
        return result > 0;
    }

    /**
     * Describe: 修改用户数据
     * Param: User
     * Return: 操作结果
     */
    public boolean update(User sysUser) {
        Integer result = userMapper.updateById(sysUser);
        return result > 0;
    }

}
